package com.service.uai.engine.comm;

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.OutputStream;
import java.net.Socket;

import com.bpms.common.CommonUtil;
import com.bpms.common.Param;
import com.bpms.eai.ISyncComm;
import com.bpms.eai.PackTool;

public class AccountQueryClient extends PackTool implements ISyncComm {

	public byte[] sendBuffer(byte[] buffer) {
		Socket st = null;
		OutputStream outputStream = null;
	
		int port = Integer.parseInt(Param.getValue("ATM", "redirect_port"));
		String ip = Param.getValue("ATM", "redirect_server");
		int timeout = Integer.parseInt(Param.getValue("ATM", "redirect_timeout"));
		
		try {
			CommonUtil.debug("AccountQueryClient.ip:" + ip + ":" + port);
			st = new Socket(ip, port);
			st.setSoTimeout(timeout * 1000);
			//CommonUtil.debug("AccountQueryClient.sendBuf(" + new String(buffer).length() + ")" + new String(buffer));
			outputStream = st.getOutputStream();
			OutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(outputStream));
			dataOutputStream.write(buffer);
			dataOutputStream.flush();
			
			byte[] response = new byte[1024];
			st.getInputStream().read(response);
			
			int len=Integer.parseInt(byteToStr(response,0,8));
			byte[] data = new byte[len+8];
			this.memcpy(data, 0, response, 0,data.length);			
			
			CommonUtil.debug("data.length " + data.length + "response data:" + new String(data));
			return data;
		} catch (Exception e) {
			CommonUtil.debug("AccountQueryClient.sendBuffer exception", e);
			return null;
		}

	}

}
